package com.google.gwt.sample.dynatablerf.client.user;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.sample.dynatablerf.client.AppController;
import com.google.gwt.sample.dynatablerf.client.user.events.GoToCategoriaViewEvent;
import com.google.gwt.sample.dynatablerf.client.user.events.GoToCategoriaViewEventHandler;
import com.google.gwt.sample.dynatablerf.client.user.events.GoToProdutoListViewEvent;
import com.google.gwt.sample.dynatablerf.client.user.events.GoToProdutoListViewEventHandler;
import com.google.gwt.sample.dynatablerf.client.user.presenters.CategoriaListPresenter;
import com.google.gwt.sample.dynatablerf.client.user.presenters.MainPresenter;
import com.google.gwt.sample.dynatablerf.client.user.presenters.ProdutoListPresenter;
import com.google.gwt.sample.dynatablerf.client.user.views.CategoriaListView;
import com.google.gwt.sample.dynatablerf.client.user.views.MainView;
import com.google.gwt.sample.dynatablerf.client.user.views.ProdutoListView;
import com.google.gwt.sample.dynatablerf.shared.AppRequestFactory;
import com.google.gwt.sample.dynatablerf.shared.CategoriaProxy;
import com.google.gwt.user.client.ui.HasWidgets;

public class UserAppController implements AppController {

	private HasWidgets rootPanel;
	private EventBus eventBus;
	private AppRequestFactory appRequestFactory;

	public UserAppController(HasWidgets rootPanel) {
		super();
		this.eventBus = new SimpleEventBus();
		this.rootPanel = rootPanel;
		createAppRequestFactory();
	}

	public void start() {
		bind();
		openMainView();
	}

	private void bind() {

		eventBus.addHandler(GoToCategoriaViewEvent.TYPE,
				new GoToCategoriaViewEventHandler() {

					@Override
					public void onGoToCategoriaView(GoToCategoriaViewEvent event) {
						openCategoriaListView(event.getHasWidgets(),
								event.getCategoriaProxy());

					}
				});

		eventBus.addHandler(GoToProdutoListViewEvent.TYPE,
				new GoToProdutoListViewEventHandler() {

					@Override
					public void onGoToProdutoListView(
							GoToProdutoListViewEvent event) {
						openProdutoListView(event.getHasWidgets(),
								event.getCategoriaProxy());

					}
				});
	}

	private void openCategoriaListView(HasWidgets container,
			CategoriaProxy categoriaProxy) {
		CategoriaListView categoriaListView = new CategoriaListView();
		CategoriaListPresenter categoriaListPresenter = new CategoriaListPresenter(
				categoriaProxy, categoriaListView, eventBus, appRequestFactory);
		categoriaListPresenter.go(container);
	}

	private void openProdutoListView(HasWidgets container,
			CategoriaProxy categoriaProxy) {
		ProdutoListView produtoListView = new ProdutoListView();
		ProdutoListPresenter produtoListPresenter = new ProdutoListPresenter(
				produtoListView, categoriaProxy);
		produtoListPresenter.go(container);
	}

	private void createAppRequestFactory() {
		appRequestFactory = GWT.create(AppRequestFactory.class);
		appRequestFactory.initialize(eventBus);
	}

	private void openMainView() {
		MainView mainView = new MainView();
		MainPresenter mainPresenter = new MainPresenter(eventBus, mainView);
		mainPresenter.go(rootPanel);
	}

}